package com.drg.center.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.drg.center.entity.param.SysPositionEditDTO;
import com.drg.center.entity.param.SysPositionListDTO;
import com.drg.center.entity.result.SysPositionExtVO;
import com.drg.center.entity.result.SysPositionVO;
import com.guaike.core.entity.exception.ApiException;
import com.guaike.core.entity.result.PageEntity;
import com.guaike.core.entity.result.Result;
import com.guaike.core.utils.ListUtil;
import com.drg.center.dao.SysPositionDAO;
import com.drg.center.entity.SysPositionPO;
import com.drg.center.service.SysPositionService;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: zhangxilin
 * @Date: 2024/4/23
 */
@Slf4j
@Service
public class SysPositionServiceImpl implements SysPositionService {

    @Autowired
    private SysPositionDAO sysPositionDAO;

    @Override
    public Result<Boolean> edit(SysPositionEditDTO param) {
        if(sysPositionDAO.isExist(param.getPosition(), param.getId())){
            return Result.failure().message("岗位名称已存在").build();
        }
        if(param.getId()==null){
            return add(param);
        }
        return update(param);
    }

    public Result<Boolean> add(SysPositionEditDTO param) {
        SysPositionPO record = new SysPositionPO();
        record.setPosition(param.getPosition());
        record.setRemark(param.getRemark());
        record.setIsLeader(param.getIsLeader());
        sysPositionDAO.save(record);
        return Result.success().data(Boolean.TRUE).build();
    }
    public Result<Boolean> update(SysPositionEditDTO param) {
        SysPositionPO record = sysPositionDAO.findById(param.getId());
        if(record==null){
            return Result.failure().message("记录不存在").build();
        }
        record.setPosition(param.getPosition());
        record.setRemark(param.getRemark());
        record.setIsLeader(param.getIsLeader());

        sysPositionDAO.updateById(record);
        return Result.success().data(Boolean.TRUE).build();
    }

    @Override
    public Result<PageEntity<SysPositionVO>> findPage(SysPositionListDTO param) {
        List<SysPositionVO> list = Lists.newArrayList();
        IPage<SysPositionPO> iPage = sysPositionDAO.findPage(param);
        if(CollectionUtils.isEmpty(iPage.getRecords())){
            return Result.success().data(new PageEntity<>(iPage.getTotal(),list)).build();
        }
        list = ListUtil.copyList(iPage.getRecords(), SysPositionVO.class);
        return Result.success().data(new PageEntity<>(iPage.getTotal(),list)).build();
    }

    @Override
    public Result<List<SysPositionVO>> all() {
        List<SysPositionVO> list = Lists.newArrayList();
        List<SysPositionPO> poList = sysPositionDAO.findAll();
        if(CollectionUtils.isEmpty(poList)){
            return Result.success().data(list).build();
        }
        list = ListUtil.copyList(poList, SysPositionVO.class);
        return Result.success().data(list).build();
    }

    @Override
    public Result<SysPositionExtVO> detail(Long id) {
        SysPositionPO record = sysPositionDAO.findById(id);
        if(record==null){
            return Result.failure().message("记录不存在").build();
        }
        SysPositionExtVO vo = new SysPositionExtVO();
        vo.setId(record.getId());
        vo.setPosition(record.getPosition());
        vo.setRemark(record.getRemark());
        vo.setCreateTime(record.getCreateTime());
        vo.setIsLeader(record.getIsLeader());

        return Result.success().data(vo).build();
    }

    @Override
    public Result<Boolean> delete(Long id) {
        SysPositionPO record = sysPositionDAO.findById(id);
        if(record==null){
            return Result.failure().message("记录不存在").build();
        }

        sysPositionDAO.del(id);
        return Result.success().data(Boolean.TRUE).build();
    }
}
